package cn.sxgan;


import cn.sxgan.entity.HrFamilyDTO;
import cn.sxgan.entity.HrPersonDTO;
import cn.sxgan.entity.PersonInfo;
import cn.sxgan.random.IdCardRandom;
import cn.sxgan.random.RandomUtils;
import cn.sxgan.utils.DateUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int perNum = 0;
        int famNum = 0;
        // 创建 Scanner 对象
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the number of employees to generate (default is 20000):");
        String perNumStr = scanner.nextLine();
        if (StringUtils.isBlank(perNumStr)) {
            perNum = 20000;
        } else {
            perNum = Integer.parseInt(perNumStr);
        }
        System.out.println("Please enter the number of family members to generate (default is 20000) (if the input value is greater than or equal to the number of employees, a random number will be generated):");
        String famNumStr = scanner.nextLine();
        if (StringUtils.isBlank(famNumStr)) {
            famNum = 20000;
        } else {
            famNum = Integer.parseInt(famNumStr);
        }
        exportHrList(perNum, famNum);
    }
    
    private static void exportHrList(int perNum, int famNum) {
        System.out.println("Generation is starting, please wait patiently......");
        List<HrPersonDTO> hrPersonDTOS = new ArrayList<>();
        List<HrFamilyDTO> hrFamilyDTOS = new ArrayList<>();
        // 构造员工数据
        for (int i = 1; i <= perNum; i++) {
            HrPersonDTO hrPersonDTO = getHrPersonDTO(i);
            hrPersonDTOS.add(hrPersonDTO);
        }
        int sqNum = 1;
        for (HrPersonDTO hrPersonDTO : hrPersonDTOS) {
            if (sqNum > famNum) {
                break;
            }
            int fmNum = (int) (Math.random() * 2 + 1);
            for (int i = 1; i <= fmNum; i++) {
                HrFamilyDTO hrFamilyDTO = getHrFamilyDTO(sqNum, hrPersonDTO);
                hrFamilyDTOS.add(hrFamilyDTO);
                sqNum++;
            }
        }
        if (hrFamilyDTOS.size() > famNum) {
            hrFamilyDTOS.subList(famNum, hrFamilyDTOS.size()).clear();
        }
        // 模板文件路径和生成的新文件路径
        String templatePath = FilePathConst.HR_EXPORT_FILE;
        File file = new File(FilePathConst.TARGET_DATA_DIR);
        if (!file.exists()) {
            file.mkdirs();
        }
        StringBuffer sb = new StringBuffer();
        sb.append(FilePathConst.ROOT_DIR)
                .append(DateUtils.dateTimeNow(), 2, 12)
                .append("_").append(hrPersonDTOS.size()).append("_")
                .append(hrFamilyDTOS.size()).append("_人员清单.xlsx");
        String newPath = sb.toString();
        // 创建 ExcelWriter 对象，指定模板文件路径和新文件路径
        ExcelWriter excelWriter = EasyExcel.write(newPath).withTemplate(templatePath).build();
        // 创建两个 WriteSheet 对象，分别对应两个 Sheet 页
        WriteSheet personSheet = EasyExcel.writerSheet(0, "员工清单").build();
        WriteSheet familySheet = EasyExcel.writerSheet(1, "家属清单").build();
        // // 写入数据到两个 Sheet 页
        excelWriter.write(hrPersonDTOS, personSheet);
        excelWriter.write(hrFamilyDTOS, familySheet);
        // 关闭 ExcelWriter
        excelWriter.finish();
        excelWriter.close();
        System.out.println("Generation completed, path:" + newPath);
    }
    
    private static HrFamilyDTO getHrFamilyDTO(int sqNum, HrPersonDTO hrPersonDTO) {
        HrFamilyDTO hrFamilyDTO = new HrFamilyDTO();
        PersonInfo newPer = IdCardRandom.getPersonInfoByID(IdCardRandom.generateRandomID());
        hrFamilyDTO.setSeqNo(String.valueOf(sqNum));
        hrFamilyDTO.setName(RandomUtils.generateChineseName());
        hrFamilyDTO.setNationality("中国");
        hrFamilyDTO.setIdType("身份证");
        hrFamilyDTO.setIdNumber(newPer.getCardNo());
        hrFamilyDTO.setIdValidFrom("2021-01-01");
        hrFamilyDTO.setIdValidTo("2031-01-01");
        hrFamilyDTO.setIsIdLongTermValid("");
        hrFamilyDTO.setPhoneNumber(newPer.getPhone());
        hrFamilyDTO.setSex(newPer.getSex());
        hrFamilyDTO.setBirthDate(newPer.getBirthDay());
        hrFamilyDTO.setDefaultPlanCode("B");
        hrFamilyDTO.setOccupationType("1");
        hrFamilyDTO.setOccupationCode("00101");
        hrFamilyDTO.setHasMedicalInsurance("有");
        hrFamilyDTO.setIsRetired("否");
        hrFamilyDTO.setRelationshipToEmployee("兄弟姐妹");
        hrFamilyDTO.setEmployeeName(hrPersonDTO.getName());
        hrFamilyDTO.setEmployeeIdType(hrPersonDTO.getIdType());
        hrFamilyDTO.setEmployeeIdNumber(hrPersonDTO.getIdNumber());
        hrFamilyDTO.setBankName("");
        hrFamilyDTO.setBankAccount("");
        hrFamilyDTO.setAccountName("");
        hrFamilyDTO.setIsRuralPopulation("是");
        hrFamilyDTO.setIsDisabled("否");
        hrFamilyDTO.setRareCharacter("");
        hrFamilyDTO.setRemark("");
        hrFamilyDTO.setIsIndividualBusinessOwner("否");
        hrFamilyDTO.setEmail(RandomUtils.createEmail());
        return hrFamilyDTO;
    }
    
    private static HrPersonDTO getHrPersonDTO(int i) {
        HrPersonDTO hrPersonDTO = new HrPersonDTO();
        PersonInfo personInfoByID = IdCardRandom.getPersonInfoByID(IdCardRandom.generateRandomID());
        hrPersonDTO.setSeqNo(String.valueOf(i));
        hrPersonDTO.setName(RandomUtils.generateChineseName());
        hrPersonDTO.setDepartment("FBL");
        hrPersonDTO.setNationality("中国");
        hrPersonDTO.setIdType("身份证");
        hrPersonDTO.setIdNumber(personInfoByID.getCardNo());
        hrPersonDTO.setIdValidFrom("2021-01-01");
        hrPersonDTO.setIdValidTo("2031-01-01");
        hrPersonDTO.setIsIdLongTermValid("");
        hrPersonDTO.setPhoneNumber(personInfoByID.getPhone());
        hrPersonDTO.setSex(personInfoByID.getSex());
        hrPersonDTO.setBirthDate(personInfoByID.getBirthDay());
        hrPersonDTO.setPlanCode("A");
        hrPersonDTO.setJobLevel("1");
        hrPersonDTO.setOccupationType("1");
        hrPersonDTO.setOccupationCode("00101");
        hrPersonDTO.setHasMedicalInsurance("有");
        hrPersonDTO.setIsRetired("否");
        hrPersonDTO.setBankName("");
        hrPersonDTO.setBankAccount("");
        hrPersonDTO.setAccountName("");
        hrPersonDTO.setIsRuralPopulation("是");
        hrPersonDTO.setIsDisabled("否");
        hrPersonDTO.setRareCharacter("");
        hrPersonDTO.setRemark("");
        hrPersonDTO.setIsIndividualBusinessOwner("否");
        hrPersonDTO.setEmail(RandomUtils.createEmail());
        return hrPersonDTO;
    }
}